package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Classname MenuServiceImpl
 * @Description TODO    菜单业务层实现类
 * @Date 2019/12/6
 * @Created by Administrator
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDao menuDao;

    /**
     * 在弹窗中展示菜单列表
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    /**
     * 弹窗回显选中菜单
     */
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }


    @Override
    public void save(Menu menu) {
        menuDao.save(menu);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> page = menuDao.findPage();
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delById(Integer id) {
        long count = menuDao.queryRelation(id);
        if (count > 0) {
            throw new RuntimeException("菜单表被角色表关联，不可删除");
        }
        menuDao.delById(id);
    }

    //根据用户名返回菜单列表
    @Override
    public List<Menu> showMenu(String username){
        return menuDao.showMenu(username);
    }
}
